﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace BanVeXemPhim_Presentation
{
    class GiaVeDAO : DataProvider
    {
        public void CapNhatGiaVe(GiaVeDTO giaveDto)
        {
            connect();
            string query = "update GiaVe set LoaiGhe = " + giaveDto.LoaiGhe.ToString()
                + ",IdDoiTuong = " + giaveDto.IdDoiTuong.ToString()
                + ",IdPhienBan = " + giaveDto.IdPhienBan.ToString()
                + ",GioKetThuc = convert(datetime,'" + giaveDto.GioKetThuc.ToShortTimeString() + "',108)"
                + ",GioBatDau = convert(datetime,'" + giaveDto.GioBatDau.ToShortTimeString() + "',108)"
                + ",GiaVe = " + giaveDto.GiaVe.ToString()
                + ",Thu = "+giaveDto.Thu.ToString()
                +" where idGiaVe = "+giaveDto.IdGiaVe.ToString();
            MessageBox.Show(query);
            executeNonQuery(query);
        }
        public void XoaGiaVe(GiaVeDTO giaveDto)
        {
            connect();
            string query = "delete from GiaVe where (loaighe = " + giaveDto.LoaiGhe.ToString() +
                " )and (idDoiTuong = " + giaveDto.IdDoiTuong.ToString() +
                " )and (idPhienBan = " + giaveDto.IdPhienBan.ToString() +
                " )and (Thu = " + giaveDto.Thu.ToString() +
                " )and (GioBatDau= convert(datetime,'" + giaveDto.GioBatDau.ToShortTimeString() + "',108)" +
                " )and (GioKetThuc= convert(datetime,'" + giaveDto.GioKetThuc.ToShortTimeString() + "',108)" + ")";

            executeNonQuery(query);
        }
        public void ThemGiaVe(GiaVeDTO giaveDto)
        {
            connect();
            string query = "insert into GiaVe(LoaiGhe,idDoiTuong,IdPhienBan,GiaVe,GioKetThuc,GioBatDau,Thu) values("+giaveDto.LoaiGhe.ToString()+ ","
                + giaveDto.IdDoiTuong.ToString() + ","
                + giaveDto.IdPhienBan.ToString() + ","
                + giaveDto.GiaVe.ToString()
                +",convert(datetime,'"
                + giaveDto.GioKetThuc.ToShortTimeString() + "',108)"
                + ",convert(datetime,'"
                + giaveDto.GioBatDau.ToShortTimeString() + "',108)"
                + "," + giaveDto.Thu.ToString()+")";
            //MessageBox.Show(query);
            executeNonQuery(query);
        }
        public bool KiemTraGiaVe(GiaVeDTO giaveDto)
        {
            connect();
            bool isExisted = false;
            string query = "select * from giave where (loaighe = " + giaveDto.LoaiGhe.ToString() +
                " )and (idDoiTuong = " + giaveDto.IdDoiTuong.ToString() +
                " )and (idPhienBan = " + giaveDto.IdPhienBan.ToString()+
                " )and (Thu = " + giaveDto.Thu.ToString()+ 
                " )and (GioBatDau= convert(datetime,'" + giaveDto.GioBatDau.ToShortTimeString()+"',108)"+
                " )and (GioKetThuc= convert(datetime,'" + giaveDto.GioKetThuc.ToShortTimeString() + "',108)" + ")";
           // MessageBox.Show(query);
            object obj = executeScalar(query);
            if (obj!=null)
            {
                isExisted = true;
            }
            return isExisted;
        }
        public GiaVeDTO[] LayDanhSachGiaVe()
        {
            connect();
            string query = "select * from giave ";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            GiaVeDTO[] dsGiaVe = new GiaVeDTO[n];
            for (int i = 0; i < n; i++)
            {
                dsGiaVe[i] = (GiaVeDTO)GetDataFromDataRow(dataTable, i);
            }
            return dsGiaVe;
        }
        protected override object GetDataFromDataRow(System.Data.DataTable dt, int i)
        {
            GiaVeDTO giave = new GiaVeDTO();
            giave.IdGiaVe = Convert.ToInt32(dt.Rows[i]["IdGiaVe"].ToString());
            giave.IdDoiTuong = Convert.ToInt32(dt.Rows[i]["IdDoiTuong"].ToString());
            giave.IdPhienBan = Convert.ToInt32(dt.Rows[i]["IdPhienBan"].ToString());
            giave.LoaiGhe = Convert.ToInt32(dt.Rows[i]["LoaiGhe"].ToString());
            giave.GiaVe= Convert.ToInt32(dt.Rows[i]["GiaVe"].ToString());
            giave.GioBatDau = Convert.ToDateTime(dt.Rows[i]["GioBatDau"].ToString());
            giave.GioKetThuc = Convert.ToDateTime(dt.Rows[i]["GioKetThuc"].ToString());
            giave.Thu = int.Parse(dt.Rows[i]["Thu"].ToString());
            return giave;
        }
    }
}
